package com.wx.adapter.http.config;

import feign.Logger;
import feign.codec.Decoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.util.ArrayList;
import java.util.List;

/**
 * wx_back_end
 *
 * @Auther: cheng.tang
 * @Date: 2023/2/10 8:28 PM
 * @Description:
 */
@Configuration
@EnableFeignClients(basePackages = "com.wx")
public class WxFeignConfig {

    @Bean
    public Logger.Level loggerLevel() {
        return Logger.Level.BASIC;
    }

//    @Bean
    public Decoder feignDecoder() {
        WxMessageConverter wxConverter = new WxMessageConverter();
        ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(wxConverter);
        return new SpringDecoder(objectFactory);
    }

    public static class WxMessageConverter extends MappingJackson2HttpMessageConverter {
        public WxMessageConverter() {
            List<MediaType> supportedMediaTypes = getSupportedMediaTypes();
            List<MediaType> mediaTypes = new ArrayList<>();
            mediaTypes.add(MediaType.TEXT_PLAIN);
            setSupportedMediaTypes(mediaTypes);
        }
    }


}
